package cn.manter.org.service.impl;

import cn.manter.basic.service.impl.BaseServiceImpl;
import cn.manter.org.domain.Department;
import cn.manter.org.mapper.DepartmentMapper;
import cn.manter.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;



    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        String dirPath ="";
        // 给dirPath拼接值
        if(department.getParent().getId()==null){
            // 判断他为顶级数据，拼接
            dirPath="/" + department.getId();
        } else{
            // 不是顶级数据，先获取到当前级的数据
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            // 根据上级部门的dirpath路径，拼接
            if(parentDept != null){
                dirPath=parentDept.getDirPath() + "/" + department.getId();
            }

        }
        // 设置到对象中
        department.setDirPath(dirPath);
        // 调用修改方法
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        String dirPath="";
        // 给dirPath拼接值
        if(department.getParent().getId()==null){
            // 判断他为顶级数据，拼接
            dirPath="/" + department.getId();
        } else{
            // 不是顶级数据，先获取到当前级的数据
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            // 根据上级部门的dirpath路径，拼接
            if(parentDept != null){
                dirPath=parentDept.getDirPath() + "/" + department.getId();
            }
        }
        // 设置到对象中
        department.setDirPath(dirPath);
        // 调用修改方法
        departmentMapper.update(department);
    }

    @Override
    public List<Department> tree() {
        // 查询出部门所有的数据
        List<Department> depts = departmentMapper.loadAll();
        // 创建一个ArrayList集合对象
        List<Department> deptTree = new ArrayList<Department>();
        // 3.将集合数据换转为map，消除时间复杂度问题
        HashMap<Long, Department> deptMap = new HashMap<>();
        for (Department department : depts) {
            deptMap.put(department.getId(), department);
        }
        // 遍历获取一级部门数据
        for (Department deptTmp : depts) {
            // 如果数据没有父级id，那么就是一级数据
            if (deptTmp.getParentId()==null){
                // 设置为顶级数据
                deptTree.add(deptTmp);
            } else {
                // 从map集合中获取到父对象，将自己添加到父对象的children集合中
                Department department = deptMap.get(deptTmp.getParentId());
                if (department != null){
                    department.getChildren().add(deptTmp);
                }
            }
        }

        return deptTree;
    }
}